package com.tdy.spring_boot.modules.test.service.impl;

import com.tdy.spring_boot.modules.common.vo.Result;
import com.tdy.spring_boot.modules.test.dao.StudentDao;
import com.tdy.spring_boot.modules.test.entity.Student;
import com.tdy.spring_boot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description StudentServiceImpl
 * @Author JiangHu
 * @Date 2022/8/19 15:05
 */
@Service
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentDao studentDao;

	@Override
	@Transactional
	public Result<Student> insertStudent(Student student) {
		studentDao.saveAndFlush(student);
		return new Result<>(Result.ResultStatus.SUCCESS.code,
				"Insert success.", student);
	}

	@Override
	@Transactional
	public Result<Student> updateStudent(Student student) {
		studentDao.saveAndFlush(student);
		return new Result<>(Result.ResultStatus.SUCCESS.code,
				"Update success.", student);
	}

	@Override
	@Transactional
	public Result<Object> deleteStudentById(int id) {
		studentDao.deleteById(id);
		return new Result<>(Result.ResultStatus.SUCCESS.code, "Delete success.");
	}

	@Override
	public Student getStudentById(int id) {
		return studentDao.getById(id);
	}
}
